﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Data;
using System.Data.Odbc;
using WLMCoreLibrary;
using System.Text;
using System.Configuration;
using System.Xml.Serialization;

namespace MWS_AddDirectory
{
    public class TagCat
    {
        private string _key;

        public string key
        {
           get;set;
        
        }
        private string _value;

        public string value
        {
           get;set;
           
        }

        public TagCat(string nuevoKey, string nuevoValue)
        {
            key = nuevoKey;
            value = nuevoValue;
        }
    }

    public class AnuncioFrases : BaseClass
    {
        #region private fields
        private String _frase;
        private int _idRedSocial;
        #endregion

        #region public properties
        public String frase {get;set; }
        public int idRedSocial {get;set; }
        #endregion

        #region constructors
        public AnuncioFrases(ref dbConnector ConexionDB)
        {
            ConexionDBInterna = ConexionDB;
            WLMClassBinder classBinder = new WLMClassBinder(ref ConexionDB);
            id = classBinder.NewRecord("ANUNCIOFRASES");
            string queryString = "";
            queryString = "SELECT * FROM ANUNCIOFRASES WHERE id='" + id + "'";
            GetRecord(queryString);
        }

        public AnuncioFrases(int registryId, ref dbConnector ConexionDB)
        {
            ConexionDBInterna = ConexionDB;
            string queryString = "";
            queryString = "SELECT * FROM ANUNCIOFRASES WHERE id='" + registryId + "'";

            this.GetRecord(queryString);
        }

        public AnuncioFrases(int idRed, bool random, ref dbConnector ConexionDB)
        {
            ConexionDBInterna = ConexionDB;
            string queryString = "SELECT * FROM ANUNCIOFRASES WHERE idRedSocial='" + idRed + "' ORDER BY RAND() LIMIT 1 ";

            this.GetRecord(queryString);
        }
        public AnuncioFrases(DataRow dataRow, ref dbConnector ConexionDB)
        {

            this.GetRecord(dataRow);
        }
        #endregion

        #region public methods
        public void Delete()
        {
            string queryString = "";
            queryString = "DELETE FROM ANUNCIOFRASES WHERE id='" + id + "'";
            ConexionDBInterna.ExecuteCommand(queryString);
        }
        #endregion

        #region Private Methods
        private void GetRecord(string queryString)
        {
            OdbcDataReader reader = ConexionDBInterna.ExecuteReader(queryString);
            if (reader.HasRows)
            {
                if (!reader.IsDBNull(reader.GetOrdinal("id")))
                    id = (System.Int32)reader["id"];
                if (!reader.IsDBNull(reader.GetOrdinal("fechaCreacion")))
                    fechaCreacion = Convert.ToDateTime(reader["fechaCreacion"]);
                if (!reader.IsDBNull(reader.GetOrdinal("fechaModificacion")))
                    fechaModificacion = Convert.ToDateTime(reader["fechaModificacion"]);
                if (!reader.IsDBNull(reader.GetOrdinal("idUsuario")))
                    idUsuario = (System.Int32)reader["idUsuario"];
                if (!reader.IsDBNull(reader.GetOrdinal("idUsuarioModificacion")))
                    idUsuarioModificacion = (System.Int32)reader["idUsuarioModificacion"];
                if (!reader.IsDBNull(reader.GetOrdinal("frase")))
                    frase = (System.String)reader["frase"];
                if (!reader.IsDBNull(reader.GetOrdinal("idRedSocial")))
                    idRedSocial = (System.Int32)reader["idRedSocial"];
            }
        }


        private void GetRecord(DataRow dataRow)
        {
            if (dataRow != null)
            {
                if (dataRow["id"] != null)
                    id = (System.Int32)dataRow["id"];
                if (dataRow["fechaCreacion"] != null)
                    fechaCreacion = (System.DateTime)dataRow["fechaCreacion"];
                if (dataRow["fechaModificacion"] != null)
                    fechaModificacion = (System.DateTime)dataRow["fechaModificacion"];
                if (dataRow["idUsuario"] != null)
                    idUsuario = (System.Int32)dataRow["idUsuario"];
                if (dataRow["idUsuarioModificacion"] != null)
                    idUsuarioModificacion = (System.Int32)dataRow["idUsuarioModificacion"];
                if (dataRow["frase"] != null)
                    frase = (System.String)dataRow["frase"];
                if (dataRow["idRedSocial"] != null)
                    idRedSocial = (System.Int32)dataRow["idRedSocial"];
            }
        }


        protected override void UpdateBind()
        {
            StringBuilder strCommand = new StringBuilder();
            strCommand.Append("UPDATE ANUNCIOFRASES set ");
            strCommand.Append("fechaModificacion='" + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + "'");
            strCommand.Append(",idUsuarioModificacion='" + idUsuarioModificacion.ToString() + "'");
            strCommand.Append(",frase=\"" + WLMFunctions.comillas(frase) + "\"");
            strCommand.Append(",idRedSocial='" + idRedSocial.ToString() + "'");
            strCommand.Append(" WHERE id='" + id + "'");
            ConexionDBInterna.ExecuteCommand(strCommand.ToString());
        }
        #endregion
    }

    public class AnuncioFrasesCollection
    {

        private BaseClassArray<AnuncioFrases> _items;
        public BaseClassArray<AnuncioFrases> Elementos {get;set; }
        public AnuncioFrasesCollection()
        {
        }
        public AnuncioFrasesCollection(ref dbConnector ConexionDB)
        {

            string strCommand = "SELECT id FROM ANUNCIOFRASES ";
            OdbcDataReader dataReader = ConexionDB.ExecuteReader(strCommand);
            Elementos = new BaseClassArray<AnuncioFrases>();
            while (dataReader.Read())
            {
                AnuncioFrases newItem = new AnuncioFrases(Convert.ToInt32(dataReader["id"]), ref ConexionDB);
                Elementos.Add(newItem);
            }
            dataReader.Close();
            dataReader.Dispose();
        }

        public AnuncioFrasesCollection(int idRedSocial, ref dbConnector ConexionDB)
        {

            string strCommand = "SELECT id FROM ANUNCIOFRASES WHERE idRedSocial='" + idRedSocial + "'";
            OdbcDataReader dataReader = ConexionDB.ExecuteReader(strCommand);
            Elementos = new BaseClassArray<AnuncioFrases>();
            while (dataReader.Read())
            {
                AnuncioFrases newItem = new AnuncioFrases(Convert.ToInt32(dataReader["id"]), ref ConexionDB);
                Elementos.Add(newItem);
            }
            dataReader.Close();
            dataReader.Dispose();
        }


    }

    public class FuncionesDirectorio
    {
        public static List<ParametroTransformacionXML> GetTransformationParameters()
        {
            WebSiteMainConfiguration config = WebSiteMainConfiguration.GetConfig();
            List<ParametroTransformacionXML> parametros = new List<ParametroTransformacionXML>();
            parametros.Add(new ParametroTransformacionXML("dedomainID", DeCurrentDomain.IdentificadorDominio.ToString()));
            parametros.Add(new ParametroTransformacionXML("skinPath", config.Settings["skinPath"].value));
            parametros.Add(new ParametroTransformacionXML("isMobile", MiWebSite_Core.MiWebSiteFunctions.isMobileBrowser().ToString().ToLower()));
            parametros.Add(new ParametroTransformacionXML("MapsAPIKey", DeCurrentDomain.MapsApiKey));
            parametros.Add(new ParametroTransformacionXML("MapPoint", DeCurrentDomain.MapPoint));
            parametros.Add(new ParametroTransformacionXML("DirectoryCity", DeCurrentDomain.Ciudad));
            parametros.Add(new ParametroTransformacionXML("idParentDomain", DeCurrentDomain.idParentDomain.ToString()));
            parametros.Add(new ParametroTransformacionXML("dedomainname", DeCurrentDomain.DomainName));
            parametros.Add(new ParametroTransformacionXML("desubdomainname", DeCurrentDomain.SubDomain));
            parametros.Add(new ParametroTransformacionXML("dedomainCity", DeCurrentDomain.Ciudad));
            parametros.Add(new ParametroTransformacionXML("twitterAccount", DeCurrentDomain.twitterAccount));
            parametros.Add(new ParametroTransformacionXML("facebookAccount", DeCurrentDomain.facebook));
            parametros.Add(new ParametroTransformacionXML("username", CurrentClient.Nombre));
            return parametros;
        }

        public static string EnviarEmail(string fromEmail, string fromName, string toEmail, string toName, string subject, string emailBody)
        {
            WebSiteMainConfiguration config = WebSiteMainConfiguration.GetConfig();
            WLM.servicios.Emailer mailer = new WLM.servicios.Emailer();
            WLM.servicios.EmailRecipient[] recipiente = new WLM.servicios.EmailRecipient[1];
            recipiente[0] = new WLM.servicios.EmailRecipient();
            recipiente[0].Email = toEmail;
            recipiente[0].Name = toName;

            //recipiente
            WLM.servicios.WebSiteProfile perfilWebsite = new WLM.servicios.WebSiteProfile();
            perfilWebsite.SMTPPassword = config.MailConfig["MailerPassword"].value;
            perfilWebsite.SMTPPort = "26";
            perfilWebsite.SMTPServer = config.MailConfig["MailerServer"].value;
            perfilWebsite.SMTPUser = config.MailConfig["MailerEmail"].value;
            perfilWebsite.WebSite = config.Settings["siteName"].value;
            perfilWebsite.ContactEmail = new WLM.servicios.EmailRecipient();
            perfilWebsite.ContactEmail.Email = fromEmail;
            perfilWebsite.ContactEmail.Name = fromName;


            return mailer.EnviarEmail(recipiente, subject, emailBody, perfilWebsite);
        }
    }

}